package com.chinackts.auth.service;

import com.chinackts.auth.model.CookieStore;
import com.chinackts.auth.repository.redis.CookieStoreRepository;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Lianhong
 * @since 2021/10/28
 */
@Repository
public class CookieStoreService {

    @Resource
    private CookieStoreRepository cookieStoreRepository;

    public List<String> getJSESSIONID(String id) {
        List<CookieStore> cookies = this.cookieStoreRepository.getCookies(id);
        if (CollectionUtils.isEmpty(cookies)) return Collections.emptyList();
        List<String> result = new ArrayList<>();
        for (CookieStore c : cookies) {
            if (c.getName().equalsIgnoreCase("JSESSIONID")) {
                result.add(c.getValue());
            }
        }
        return result;
    }

}
